package com.wa.service.impl;
import com.wa.api.ILogMessageApi;
import com.wa.bean.BasicAllchipsLog;
import com.wa.enums.BaseResponse;
import com.wa.enums.LogTableEnum;
import com.wa.pojo.BasicAllchipsLogPojo;
import com.wa.pojo.GenericResp;
import com.wa.util.CommUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 日志消息接口实现
 * @Author Dawn
 * @Date 2021/10/19 14:36
 */
@Component
public class LogMessageApiImpl implements ILogMessageApi {

    @Resource
//    private KafkaTemplate<String, String> kafkaTemplate;

    @Value("${spring.allchips.log.kafka.topic}")
    private String approvalEventTopic;

    /**
     * 发送日志消息
     * @Author Dawn
     * @Date 2021/10/19 14:53
     */
    @Override
    public GenericResp sendLogMsg(BasicAllchipsLogPojo basicAllchipsLogPojo){
        if (basicAllchipsLogPojo == null
                || basicAllchipsLogPojo.getLogTableEnum() == null
                || CollectionUtils.isEmpty(basicAllchipsLogPojo.getBasicAllchipsLogs())){
            return new GenericResp(BaseResponse.ERROR_RESULT.getResponseCode(), "消息不能为空");
        }

//        kafkaTemplate.send(approvalEventTopic, JsonUtils.toJson(basicAllchipsLogPojo)).addCallback(new LogListenableFutureCallback());
        return new GenericResp(BaseResponse.SUCCESS_RESULT.getResponseCode(), BaseResponse.SUCCESS_RESULT.getResponseMsgCn());
    }

    /**
     * 发送日志消息
     * @Author Dawn
     * @Date 2021/10/19 14:56
     */
    @Override
    public GenericResp sendLogMsg(BasicAllchipsLog allchipsLog, LogTableEnum logTable){
        if (allchipsLog == null){
            return new GenericResp(BaseResponse.ERROR_RESULT.getResponseCode(), "消息不能为空");
        }
        List<BasicAllchipsLog> allchipsLogs = new ArrayList<>();
        allchipsLogs.add(allchipsLog);
        BasicAllchipsLogPojo basicAllchipsLogPojo = new BasicAllchipsLogPojo();
        basicAllchipsLogPojo.setBasicAllchipsLogs(allchipsLogs);
        basicAllchipsLogPojo.setLogTableEnum(logTable);
        return this.sendLogMsg(basicAllchipsLogPojo);
    }

    /**
     * 发送日志消息
     * @Author Dawn
     * @Date 2021/10/19 14:53
     */
    @Override
    public GenericResp sendLogMsg(List<BasicAllchipsLogPojo> basicAllchipsLogPojos){
        if (CollectionUtils.isEmpty(basicAllchipsLogPojos)){
            return new GenericResp(BaseResponse.ERROR_RESULT.getResponseCode(), "消息不能为空");
        }

        for (BasicAllchipsLogPojo basicAllchipsLogPojo : basicAllchipsLogPojos){
            if (CommUtils.isNull(basicAllchipsLogPojo) || CollectionUtils.isEmpty(basicAllchipsLogPojo.getBasicAllchipsLogs())) {
                continue;
            }

            GenericResp genericResp = this.sendLogMsg(basicAllchipsLogPojo);
            if (genericResp.getCode() != 0){
                return genericResp;
            }
        }
        return new GenericResp(BaseResponse.SUCCESS_RESULT.getResponseCode(), BaseResponse.SUCCESS_RESULT.getResponseMsgCn());
    }

}
